package com.hnas.appLogin.service.impl;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnas.appLogin.dao.VerifyCodeDao;
import com.hnas.appLogin.orm.VerifyCode;
import com.hnas.appLogin.service.VerifyCodeService;
import com.hnas.appLogin.web.VerifyCodeControll;
import com.hnas.common.util.CommonUtils;
import com.hnas.common.util.ResultCode;
import com.hnas.core.app_common.util.DTOResult;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;

@Component("verifyCodeService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class VerifyCodeServiceImpl implements VerifyCodeService{
	
	private static Log log = LogFactory.getLog(VerifyCodeControll.class);
	
	private static ObjectMapper objectMapper = new ObjectMapper();
	
	@Value("#{component['verification_time']}")
	private String verificationTime;

	@Autowired
	private VerifyCodeDao verifyCodeDao;
	
	
	@Override
	public VerifyCode save(VerifyCode vo) throws Exception {
		vo.setId(CommonUtils.getUUID());
		vo.setCreateTime(DateUtil.dateTimeToString(new Date()));
		vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
		verifyCodeDao.persist(vo);
		return vo;
	}
	
	
	@Override
	public VerifyCode findNewBestCodeByPhoneNo(String phoneNo) {
		return verifyCodeDao.findNewBestCodeByPhoneNo(phoneNo);
	}


	@Override
	public void deleteByPhoneNo(String phoneNo) {
		verifyCodeDao.deleteByPhoneNo(phoneNo);
	}

	@Override
	public String sendVerifyCode(String phoneNo, String type) {
		DTOResult dtoResult = new DTOResult();
		try {
			String string = verifyCodeDao.sendVerifyCode(phoneNo, type, verificationTime);
			if(null==string){
				 dtoResult.setResultCode(ResultCode.ERROR_005);
			     dtoResult.setResultMsg(ResultCode.getResultValue(ResultCode.ERROR_005));
			     return objectMapper.writeValueAsString(dtoResult);
			}else{
				return string;
			}
		} catch (Exception e) {
			dtoResult.setResultCode(ResultCode.ERROR_005);
		     dtoResult.setResultMsg(ResultCode.getResultValue(ResultCode.ERROR_005));
			try {
				return objectMapper.writeValueAsString(dtoResult);
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
				return "";
			}
		}
	}


	@Override
	public boolean checkVerifyCode(String phoneNo, String verifyCode) {
		VerifyCode  sysCode = verifyCodeDao.findNewBestCodeByPhoneNo(phoneNo);
		if(null!=sysCode&&StringUtil.isNotEmpty(sysCode.getVerifyCode())){
			if(sysCode.getVerifyCode().equals(verifyCode)){
				//将原来的验证码变成无效
				verifyCodeDao.deleteByPhoneNo(phoneNo);
				return true;
			}
		}
		return false;
	}

	

}
